package com.bbs.service.impl;

import com.bbs.constant.MyConstant;
import com.bbs.dao.PostDao;
import com.bbs.dao.UserDao;
import com.bbs.domain.ResultInfo;
import com.bbs.entity.Post;
import com.bbs.entity.Reply;
import com.bbs.entity.User;
import com.bbs.service.UserService;
import com.bbs.utils.MailUtils;
import com.bbs.utils.MyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpSession;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpSession;
import java.util.*;

@Component("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private PostDao postDao;

    private Map<String, HttpSession> sessionMap = new HashMap<>();


    @Override
    public User findById(int id) {
        return null;
    }

    @Override
    public List<User> listUserByHot() {
        List<User> userList = new ArrayList<>();
        userList = userDao.listUserByHot();
        return userList;
    }

    @Override
    public List<User> listUserByTime() {
        List<User> userList = new ArrayList<>();
        userList = userDao.listUserByTime();
        return userList;
    }

    @Override
    public int register(User user) {
        //构造user，设置未激活
        user.setActived(0);
        String activateCode = MyUtil.createActivateCode();
        user.setActivateCode(activateCode);//|随机不能重复的激活码|
        user.setJoinTime(MyUtil.formatDate(new Date()));// 注册时间
        user.setUsername("JM" + new Random().nextInt(10000)+" 号");//用户名，JM+随机数
        user.setHeadUrl(MyConstant.QINIU_IMAGE_URL);//用户头像地址

        //判断邮箱是否存在
        User user2 = userDao.findByEmail(user.getEmail());
        if (user2 != null) {
            return 2;
        }
        //执行sql插入语句,往数据库中插入用户信息的数据,insertUser方法返回值是int类型
        int flag = userDao.insertUser(user);

        if (flag == 1) {
            // 注册成功发送激活邮件
            String activtUrl = String.format("http://localhost:8080/ssm_bbs_war/activatUser?email=%s&activateCode=%s", user.getEmail(), activateCode);

            MailUtils.sendMail(user.getEmail(), "激活链接 " + activtUrl, "用户激活邮件");
        }

        return flag;
    }

    @Override
    public ResultInfo doLogin(String email, String password, HttpSession session) {




        User user = userDao.findByEmail(email);
        ResultInfo resultInfo = new ResultInfo();
        if (user == null) {
            resultInfo.setCode("1");
            resultInfo.setMsg("用户不存在!!");
            return resultInfo;
        }

        if (!Objects.equals(user.getPassword(), password)) {
            resultInfo.setCode("1");
            resultInfo.setMsg("邮箱或密码不正确!!");
            return resultInfo;
        }

        if (!Objects.equals(user.getActived(), 1)) {
            resultInfo.setCode("1");
            resultInfo.setMsg("用户未激活!!");
            return resultInfo;
        }

        session.setAttribute("uid", user.getId());// 登录成功把个人 信息存入会话
        session.setAttribute("headUrl", user.getHeadUrl());

        resultInfo.setCode("0");
        resultInfo.setMsg("登录成功");

        return resultInfo;
    }

    @Override
    public User activatUser(String email, String activateCode) {
        User user = userDao.findByEmail(email);
        if (user == null || !Objects.equals(user.getActivateCode(), activateCode)) {
            return null;
        }

        userDao.activatUserById(user.getId());

        return user;
    }

    //更新帖子浏览次数
    @Override
    public int updateUserPostCount(Reply reply) {
        return userDao.updateUserPostCount(reply);
    }

    //点赞+1
    @Override
    public int updateUserLikeCount(Integer uid) {
        return userDao.updateUserLikeCount(uid);
    }
    //点赞-1
    @Override
    public int updateUserLikeCount2(Integer uid) {
        return userDao.updateUserLikeCount2(uid);
    }

    //查询个人主页的用户信息
    @Override
    public User selectUser(int uid) {
        return userDao.selectUser(uid);
    }

    //更新用户信息
    @Override
    public void updateUser(User user) {
        userDao.updateUser(user);
    }

    @Override
    public User loginSelectUser(String email) {
        return userDao.loginSelectUser(email);
    }

    //编辑个人信息页面
    @Override
    public User xinxiuser(int uid){
        User user=userDao.xinxiuser(uid);
        return user;
    }

}
